//
//  CallRecordListViewController.m
//  NetPhone
//
//  Created by intel intel on 12-4-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CallRecordListViewController.h"
#import "NetPhoneDAO.h"
#import "CallRecordListCell.h"
#import "Common.h"

@implementation CallRecordListViewController
@synthesize callRecord;


-(id)initWithCallRecord:(CallRecord *)_record
{
    if (self = [super init]) {
        self.callRecord = _record;
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = callRecord.phoneName;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {
        self.view.bounds=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
    rows = [[NSMutableArray alloc]init];
    NSArray *array = [[NetPhoneDAO sharedInstance]loadCallRecordWidthNumber:callRecord.phoneNum name:callRecord.phoneName];
    [rows addObjectsFromArray:array];
   
    dataTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) style:UITableViewStyleGrouped];
	dataTableView.dataSource = self;
	dataTableView.delegate = self;
    //dataTableView.backgroundColor = [UIColor whiteColor];
    dataTableView.backgroundView = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sms_reg_bg"]]autorelease];
    //dataTableView.separatorColor = [UIColor grayColor];
	[self.view addSubview:dataTableView];
    
    
    UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc]initWithTitle:@"清除" 
                                                                   style:UIBarButtonItemStylePlain 
                                                                  target:self action:@selector(clearRecord:)]autorelease];	
	self.navigationItem.rightBarButtonItem = rightButton;

}

-(void)reloadCallRecords
{
    [rows removeAllObjects];
    NSArray *array = [[NetPhoneDAO sharedInstance]loadCallRecordWidthNumber:callRecord.phoneNum name:callRecord.phoneName];
    [rows addObjectsFromArray:array];
    [dataTableView reloadData];
}


-(void)clearRecord:(id)sender
{
    UIActionSheet *menu = [[UIActionSheet alloc] 
                           initWithTitle: nil
                           delegate:self
                           cancelButtonTitle:@"取消"
                           destructiveButtonTitle:@"清除此人所有通话记录"
                           otherButtonTitles:nil];
    menu.tag = 1000;
    [menu showInView:self.view.superview];
    [menu release];
}
-(void)showCallTypeView
{
    NSString* callway = [[NSUserDefaults standardUserDefaults]objectForKey:@"DirectDial"];
    if (callway&& [callway isEqualToString:@"1"])
    {
        UIActionSheet* callMethdSheet1=[[UIActionSheet alloc]initWithTitle:@"温馨提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"直拨" otherButtonTitles:@"回拨", nil];
        [callMethdSheet1 showInView:self.view];
    }else{
        ////拨打类型 0.直拨  1.回拨
       [Common callWithNumber:self.personPhone name:self.userName AndTheType:1];
    }
    
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (actionSheet.tag == 1000)
    {
        if (buttonIndex == 0) {
            [[NetPhoneDAO sharedInstance]deleteCallRecrod:callRecord.phoneNum name:callRecord.phoneName];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else
    {
        if (buttonIndex<=1)
        {
            if (self.userName && self.userName.length>0)
            {
                [Common callWithNumber:self.personPhone name:self.userName AndTheType:buttonIndex];
            }else
            {
                [Common callWithNumber:self.personPhone name:self.personPhone AndTheType:buttonIndex];
            }
        }
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView 
{ 
	return 1; 
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
{
    return [rows count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)]autorelease];
    
    UIImageView *tagView = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"contacts"]]autorelease];
    tagView.contentMode = UIViewContentModeCenter;
    tagView.frame = CGRectMake(10, 0, 60, 60);
    [headerView addSubview:tagView];
    
    UILabel *nameLabel = [[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 150, 23)]autorelease];
    nameLabel.font = [UIFont boldSystemFontOfSize:24];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.text = callRecord.phoneName;
    [headerView addSubview:nameLabel];
    
    UILabel *timeLabel = [[[UILabel alloc]initWithFrame:CGRectMake(70, 33, 150, 20)]autorelease];
    timeLabel.font = [UIFont boldSystemFontOfSize:16];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.text = callRecord.phoneNum;
    [headerView addSubview:timeLabel];

    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return  60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSUInteger row=[indexPath row]; 
    CallRecord *_callRecord = [rows objectAtIndex:row];
    static NSString *defaultCellIdentifier = @"defaultCellIdentifier";
    CallRecordListCell *cell = (CallRecordListCell *)[tableView dequeueReusableCellWithIdentifier:defaultCellIdentifier];
    if (cell == nil) {
        cell = [[[CallRecordListCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:defaultCellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    [cell setCallRecord:_callRecord];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	NSUInteger row=[indexPath row];
    CallRecord *_callRecord = [rows objectAtIndex:row];
    self.personPhone = _callRecord.phoneNum;
    self.userName = _callRecord.phoneName;
    [self showCallTypeView];
    //[Common callWithNumber:_callRecord.phoneNum name:_callRecord.phoneName];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc
{
    [rows release];
    [callRecord release];
    [super dealloc];
}

@end
